package com.beiding.workordersystem.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.customer.dao.CustomerDao;
import com.beiding.workordersystem.customer.entity.CustomerEntity;
import com.beiding.workordersystem.customer.observer.CustomerObserver;
import com.beiding.workordersystem.customer.query.CustomerQuery;
import com.beiding.workordersystem.customer.service.CustomerService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class CustomerServiceImpl extends BaseService implements CustomerService, InitializingBean {

    @Autowired
    private CustomerDao customerDao;

    @Autowired(required = false)
    private List<CustomerObserver> customerObservers;

    @Override
    public void save(CustomerEntity customerEntity) {
        if (fillId(customerEntity)) {
            customerDao.insert(customerEntity);
        } else {
            customerDao.updateById(customerEntity);
        }
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        if (ids.length > 0) {
            for (String id : ids) {
                delete(id);
            }
        }
    }

    private void delete(String id) {
        CustomerEntity entity = customerDao.selectById(id);
        if (entity != null) {
            for (CustomerObserver observer : customerObservers) {
                observer.beforeDelete(entity.getId(), entity.getName() + "(" + entity.getCompany() + ")");
            }
            customerDao.deleteById(id);
        }
    }

    @Override
    public IPage<CustomerEntity> page(MPage page, CustomerQuery customerEntity) {

        LambdaQueryWrapper<CustomerEntity> wrapper = new QueryWrapper<CustomerEntity>().lambda();

        //不展示自定义的客户

        if (customerEntity.getCustom() != null) {
            wrapper.eq(CustomerEntity::getCustom, customerEntity.getCustom());
        }

        if (notEmpty(customerEntity.getSearch_NameOrCompany())) {
            wrapper.like(CustomerEntity::getName, customerEntity.getSearch_NameOrCompany()).or().like(CustomerEntity::getCompany, customerEntity.getSearch_NameOrCompany());
        } else {
            if (notEmpty(customerEntity.getName())) {
                wrapper.like(CustomerEntity::getName, customerEntity.getName());
            }
            if (notEmpty(customerEntity.getCompany())) {
                wrapper.like(CustomerEntity::getCompany, customerEntity.getCompany());
            }
            if (notEmpty(customerEntity.getRank())) {
                wrapper.eq(CustomerEntity::getRank, customerEntity.getRank());
            }
        }

        return customerDao.selectPage(new Page<>(page.getCurrentPage(), page.getPageSize()), wrapper);
    }

    @Override
    public IPage<CustomerEntity> findByIds(MPage vo, List<String> ids) {
        return customerDao.selectPage(vo.toIPage(), new QueryWrapper<CustomerEntity>().lambda().in(CustomerEntity::getId, ids));
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        if (customerObservers == null) {
            customerObservers = Collections.EMPTY_LIST;
        }
    }
}
